package com.weikuang.fareast.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.trello.rxlifecycle2.android.ActivityEvent;
import com.weikuang.fareast.R;
import com.weikuang.fareast.adapter.DistributionSendAdapter;
import com.weikuang.fareast.bean.DistributionSendBean;
import com.weikuang.fareast.bean.OfficeBean;
import com.weikuang.fareast.bean.OfficeRequireBean;
import com.weikuang.fareast.bean.SimpleBean;
import com.weikuang.fareast.http.MainHttp;
import com.weikuang.fareast.utils.UserHelper;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.dlc.commonlibrary.okgo.rx.OkObserver;
import cn.dlc.commonlibrary.ui.widget.CommonItemView;
import cn.dlc.commonlibrary.ui.widget.TitleBar;
import cn.dlc.commonlibrary.ui.widget.popup.CommonPopupList;
import cn.dlc.commonlibrary.utils.ToastUtil;
import cn.dlc.commonlibrary.utils.rv_tool.RecyclerViewUtil;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

//配送中心布草科室分发

public class HotelSendActivity extends BaseActivity {
    @BindView(R.id.mTitleBar)
    TitleBar mTitleBar;

    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.tv_offic)
    TextView tvOffic;
    @BindView(R.id.drop_offic)
    FrameLayout dropOffic;
    @BindView(R.id.item_offic)
    CommonItemView itemOffic;
    @BindView(R.id.number)
    TextView number;

    private CommonPopupList mOfficPopup;
    private List<OfficeBean.DataBean> mOfficList;
    private DistributionSendAdapter mDistributionSendAdapter;
    String offic_id = "";
    List<DistributionSendBean> datasList = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_distribution_center;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_distribution_center);
        ButterKnife.bind(this);
        String mhotel_name = UserHelper.get().gethotel()+"配送中心布草科室分发";
        mTitleBar.setTitle(mhotel_name);
        InitRecyclerView();
        getOffice();
    }

    private void InitRecyclerView() {
        mDistributionSendAdapter = new DistributionSendAdapter();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        RecyclerViewUtil.setDefaultDivider(mRecyclerView, linearLayoutManager);
        mRecyclerView.setAdapter(mDistributionSendAdapter);

        mOfficPopup = CommonPopupList.newBuilder(this)
                .setWidthEquals(dropOffic)
                .setOnSelectItemListener(new CommonPopupList.OnSelectItemListener() {
                    @Override
                    public void onSelectItem(CommonPopupList popup, String item, int position) {
                        popup.dismiss();
                        tvOffic.setText(item);
                        if (mOfficList != null) {
                            offic_id = mOfficList.get(position).id;
                            officeRequire();
                        }
                    }
                })
                .build();

    }


    @OnClick({R.id.drop_offic, R.id.btn_read})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.drop_offic:
                mOfficPopup.showAsDropDown(view);
                break;
            case R.id.btn_read:
                divideCommit();
                break;
        }
    }

    private void getOffice() {
        MainHttp.get().getOffice("","")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(bindUntilEvent(ActivityEvent.DESTROY))
                .subscribe(new OkObserver<OfficeBean>() {
                    @Override
                    public void onSuccess(OfficeBean officeBean) {
                        List<String> datas = new ArrayList<>();
                        mOfficList = officeBean.data;
                        for (OfficeBean.DataBean bean : officeBean.data) {
                            datas.add(bean.title);
                        }
                        mOfficPopup.setData(datas);
                    }
                    @Override
                    public void onFailure(String message, Throwable tr) {
                        ToastUtil.show(getActivity(),message);
                    }
                });
    }

    private void officeRequire() {
        MainHttp.get().officeRequire(offic_id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(bindUntilEvent(ActivityEvent.DESTROY))
                .subscribe(new OkObserver<OfficeRequireBean>() {
                    @Override
                    public void onSuccess(OfficeRequireBean officeBean) {

                        datasList.clear();
                        for (int i = 0; i < officeBean.data.detail.size();i++) {
                            DistributionSendBean bean1 = new DistributionSendBean(officeBean.data.hotel, officeBean.data.office,officeBean.data.team, officeBean.data.detail.get(i).title,
                                    officeBean.data.detail.get(i).goods_id, Integer.valueOf(officeBean.data.detail.get(i).amount));
                            datasList.add(bean1);
                        }
                        mDistributionSendAdapter.setNewData(datasList);
                        number.setText(officeBean.data.orderno);
                    }
                    @Override
                    public void onFailure(String message, Throwable tr) {
                        ToastUtil.show(getActivity(),message);
                    }
                });
    }


    private void divideCommit(){
        String goods_id = "",  nums = "";
        clearNums();
        for (int i = 0; i < datasList.size(); i++) {
            goods_id = goods_id + datasList.get(i).id + ",";
        }
        if (!TextUtils.isEmpty(goods_id)) {
            goods_id = goods_id.substring(0, goods_id.length() - 1);
        } else {
            goods_id = "";
        }

        for (int i = 0; i < datasList.size(); i++) {
            nums = nums + datasList.get(i).num + ",";
        }
        if (!TextUtils.isEmpty(goods_id)) {
            nums = nums.substring(0, nums.length() - 1);
        } else {
            nums = "";
        }
        showWaitingDialog("请等待",true);
        MainHttp.get().divideCommit(offic_id,goods_id,nums,number.getText().toString())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(bindUntilEvent(ActivityEvent.DESTROY))
                .subscribe(new OkObserver<SimpleBean>() {
                    @Override
                    public void onSuccess(SimpleBean bean) {
                        ToastUtil.show(HotelSendActivity.this,bean.msg);
                        dismissWaitingDialog();
                        datasList.clear();
                        mDistributionSendAdapter.setNewData(datasList);
                    }
                    @Override
                    public void onFailure(String message, Throwable tr) {
                        ToastUtil.show(HotelSendActivity.this,message);
                        dismissWaitingDialog();
                    }
                });
    }

    /**
     * 是否清空为0数据
     * @return
     */
    private void clearNums(){
        Iterator<DistributionSendBean> ite = datasList.iterator();
        while(ite.hasNext()){
            DistributionSendBean str = (DistributionSendBean)ite.next();
            if(str.num == 0){
                ite.remove();
            }
        }
    }
}
